package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.WeeklyActiveTask;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.WeeklyPay;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.WeeklyTargetTask;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.WeeklyWish;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.Point;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.module.task.TaskHandle;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

public class ActiveModule_周卡 extends PlayerModule {

	//bean -----------------------------------------------------------	
	public static final int acttype = 19;
	
	private int day;			//第几天
	private DateTime update;	//刷新时间
	private int pay;			//充值金额
	
	private WeeklyPay wp;		//支付
	private WeeklyWish ww;		//许愿
	private WeeklyActiveTask wat;	//活跃任务
	private WeeklyTargetTask wtt;	//目标任务
	
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	public DateTime getUpdate() {
		return update;
	}
	public void setUpdate(DateTime update) {
		this.update = update;
	}
	public int getPay() {
		return pay;
	}
	public void setPay(int pay) {
		this.pay = pay;
	}
	public WeeklyPay getWp() {
		return wp;
	}
	public void setWp(WeeklyPay wp) {
		this.wp = wp;
	}
	public WeeklyWish getWw() {
		return ww;
	}
	public void setWw(WeeklyWish ww) {
		this.ww = ww;
	}
	public WeeklyActiveTask getWat() {
		return wat;
	}
	public void setWat(WeeklyActiveTask wat) {
		this.wat = wat;
	}
	public WeeklyTargetTask getWtt() {
		return wtt;
	}
	public void setWtt(WeeklyTargetTask wtt) {
		this.wtt = wtt;
	}
	
	//func ------------------------------------------------------------
	
//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
	
	/** 添加充值经验 **/
	public void addPay(int addpay) {
		int pay = getPay();
		pay = pay + addpay;
		setPay(pay);
		set();
	}
	
	/** 创建实例 **/
	private static ActiveModule_周卡 create(String playerCode) {
		PlayerBean pb = PlayerBean.get(playerCode);
		ServerBean sb = ServerBean.get(pb.getServerId());
		activeControl sai = activeControl.get(sb.getAreaId(), acttype);
		if(sai != null) {
			ActiveModule_周卡 am = new ActiveModule_周卡();
			am.setDay(1);
			am.setPlayerCode(playerCode);
			am.setPay(0);
			am.setTimeline(sai.getTimeline());
			am.setUpdate(DateTime.now());
			
			am.setWp(WeeklyPay.create(playerCode));
			am.setWw(WeeklyWish.create(playerCode));
			am.setWtt(WeeklyTargetTask.create(playerCode));
			am.setWat(WeeklyActiveTask.create(playerCode));
			
			am.set();
			
			//活动登陆统计 TODO
			TaskHandle.handle(playerCode, TaskType.统计型_登录_次数, 1);
			return am;
		}
		
		return null;
	}
	
	//刷新 重新开启
	private void reset() {
		//清除货币
		long point1 = Point.get(getPlayerCode(), CurrencyType.CT_周卡_货币1,false);
		long point2 = Point.get(getPlayerCode(), CurrencyType.CT_周卡_货币2,false);
		try {
			Point.minus(getPlayerCode(), CurrencyType.CT_周卡_货币1, point1);
			Point.minus(getPlayerCode(), CurrencyType.CT_周卡_货币2, point2);
		} catch (CurrencyNotEnoughException e) {
			e.printStackTrace();
		}
		
		setDay(1);
		setPay(0);
		setUpdate(DateTime.now());
		
		getWp().reset(getPlayerCode());
		getWw().reset(getPlayerCode());
		getWat().reset(getPlayerCode());
		getWtt().reset(getPlayerCode());
		
		set();
		
		TaskHandle.handle(getPlayerCode(), TaskType.统计型_登录_次数, 1);
	}
	
	/** 更新 **/
	private void update() {
		PlayerBean pb = PlayerBean.get(getPlayerCode());
		ServerBean sb = ServerBean.get(pb.getServerId());
		activeControl sai = activeControl.get(String.valueOf(sb.getAreaId()), String.valueOf(acttype));
		if(sai == null) {
			return;
		}
		
		//关闭活动
		if(!sai.running() && running()) {
			setTimeline(sai.getTimeline());
			set();
		}
		
		//开启活动
		if(sai.running() && !running()) {
			setTimeline(sai.getTimeline());
			reset();
		}
		
		//更新
		DateTime now = DateTime.now();
		int days = ActiveUtil.days(getUpdate(), now);
		if(days >= 1) {
			setDay(getDay() + days);
			setUpdate(DateTime.now());
			
			//TODO: 刷新下属模块
			getWp().refresh();
			getWw().refresh();
			getWat().refresh(getPlayerCode());
			getWtt().refresh();
			
			set();
			TaskHandle.handle(getPlayerCode(), TaskType.统计型_登录_次数, 1);
		}
		
		{
			getWp().update();
			getWw().update(getPlayerCode(), getPay());
			getWat().update();
			getWtt().update(getPlayerCode());
			set();
		}
	}
	
	/**
	 * 能否购买
	 * @param goodsid
	 * @return
	 */
	public int canbuy(int goodsid) {
		return getWp().canbuy(goodsid);
	}	
	
	/**
	 * 锁单
	 * @param goodsid
	 * @param myorderid
	 */
	public void lockorder(int goodsid, String myorderid) {
		getWp().lockorder(goodsid, myorderid);
		set();
	}
	
	/**
	 * 解锁单
	 * @param goodsid
	 */
	public void unlockorder(int goodsid, String myorderid) {
		getWp().unlockorder(goodsid, myorderid);
		set();
	}
	
	/**
	 * 处理购买
	 * @param goodsid
	 * @param myorderid
	 * @param addpay
	 */
	public void handlebuy(int goodsid, String myorderid, int addpay) {
		int newpay = getPay() + addpay;
		setPay(newpay);
		getWp().handlebuy(goodsid, myorderid, getPlayerCode());
		set();
	}
	
	//redis -----------------------------------------------
	
	/** 获取redis key **/
	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	/** 保存 **/
	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取ActiveModule_征战日志
	 * @param playerCode
	 * @return
	 */
	static public ActiveModule_周卡 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_周卡 module = getv(key(playerCode));
			if (module == null) {

				//新用户不需要去查数据库
				if(!isNewPlayer){
					module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_周卡.class);
					if(module != null){module.set();}//同步到redis
				}

				if (module == null) {
					module = create(playerCode);
				}
				if(module == null){return null;}
			}

			module.update();
			module.check();

			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/** 检查红点，活动是否开启 **/
	public void check() {
		
		if(running()) {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
		}else {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.关闭);
		}
		
		if(running()){
			getWp().check();
			getWw().check(getPlayerCode());
			getWat().check(getPlayerCode());
			getWtt().check(getPlayerCode());
		}else {
			new TipHandle().updateTip(getPlayerCode(), TipType.活动_周卡_许愿未锁定, false);
			new TipHandle().updateTip(getPlayerCode(), TipType.活动_周卡_活跃任务, false);
			new TipHandle().updateTip(getPlayerCode(), TipType.活动_周卡_目标任务奖励, false);
		}
		
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_周卡 activeModule_周卡 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_周卡);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	//lock --------------------------------------------------------
	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
